package com.own.toyexchange;

/*
 * 修改玩具信息的activity
 * 用户可修改玩具的照片、名称、新旧程度、详细描述
 */

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.bjtu.toyexchange.R;

public class ModOwnToyInfo extends Activity implements OnClickListener{
	private static final String TAG = "uploadImage";
    private static String requestURL = "http://www.echuandan.com/geobdImage";
    private Button mod_toy, fanhui;
    private ImageView imageView, cho_pic;
    private EditText toy_name, toy_deg, toy_det;
    private String toyname, toydeg, toydet, username;
     
    private String picPath = "1";
     
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mod_toy_info);
        
        //接受到用户名
        Intent intent = this.getIntent();
		username = intent.getStringExtra("username");
		
		//得到玩具的名称、新旧程度、详细描述
		toyname = intent.getStringExtra("name");
		toydeg = intent.getStringExtra("deg");
		toydet = intent.getStringExtra("detail");
		
		//设置修改前的玩具的名称、新旧程度、详细描述
		toy_name = (EditText)ModOwnToyInfo.this.findViewById(R.id.mod_toy_name);
		toy_name.setText(toyname);
		toy_deg = (EditText)ModOwnToyInfo.this.findViewById(R.id.mod_toy_deg);
		toy_deg.setText(toydeg);
		toy_det = (EditText)ModOwnToyInfo.this.findViewById(R.id.mod_toy_det);
		toy_det.setText(toydet);
        
		//设置三个按钮，并添加事件监听器
        cho_pic = (ImageView) this.findViewById(R.id.mod_cho_pic);
        cho_pic.setOnClickListener(this);
        
        mod_toy = (Button) this.findViewById(R.id.mod_toy_btn);
        mod_toy.setOnClickListener(this);
        
        fanhui = (Button) this.findViewById(R.id.btnbackmodtoy);
        fanhui.setOnClickListener(this);
        
        imageView = (ImageView) this.findViewById(R.id.mod_cho_pic);
    }
     
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        
        //从相册中选择玩具的照片的按钮
        case R.id.mod_cho_pic:            
             //这个是调用android内置的intent，来过滤图片文件   ，同时也可以过滤其他的             
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(intent, 1);
            
//            //上传照片
//            File file = new File(picPath);
//            if(file!=null)
//            {
//                String request = UploadUtil.uploadFile( file, requestURL);
//                cho_pic.setText(request);
//            }
            
            break;
            
        //返回按钮
        case R.id.btnbackmodtoy:
        	ModOwnToyInfo.this.finish();
        	break;
            
        //修改玩具信息的按钮
        case R.id.mod_toy_btn:
            
            //得到输入的玩具的名称，新旧程度，详细描述
            toy_name = (EditText)ModOwnToyInfo.this.findViewById(R.id.mod_toy_name);
    		toyname = toy_name.getText().toString();
    		
    		toy_deg = (EditText)ModOwnToyInfo.this.findViewById(R.id.mod_toy_deg);
    		toydeg = toy_deg.getText().toString();
    		
    		toy_det = (EditText)ModOwnToyInfo.this.findViewById(R.id.mod_toy_det);
    		toydet = toy_det.getText().toString();
        	
        	//输入的信息的非空校验
        	if (("").equals(toyname) || null == toyname) {
				Toast.makeText(ModOwnToyInfo.this,"玩具名称不能为空...",100).show();
				return;
			}
			else if (("").equals(toydeg) || null == toydeg) {
				Toast.makeText(ModOwnToyInfo.this,"玩具新旧程度不能为空...",100).show();
				return;
			}
			else if (("").equals(toydet) || null == toydet) {
				Toast.makeText(ModOwnToyInfo.this,"玩具详细描述不能为空...",100).show();
				return;
			}
			else {
				Toast.makeText(ModOwnToyInfo.this, "发布玩具"+toyname+"成功...", 200).show();
			}
        	
            break;
            
        default:
            break;
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode==Activity.RESULT_OK)
        {
            /**
             * 当选择的图片不为空的话，在获取到图片的途径 
             */
            Uri uri = data.getData();
            Log.e(TAG, "uri = "+ uri);
            try {
                String[] pojo = {MediaStore.Images.Media.DATA};
                 
                Cursor cursor = managedQuery(uri, pojo, null, null,null);
                if(cursor!=null)
                {
                    ContentResolver cr = this.getContentResolver();
                    int colunm_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    cursor.moveToFirst();
                    String path = cursor.getString(colunm_index);
                    /***
                     * 这里加这样一个判断主要是为了第三方的软件选择，比如：使用第三方的文件管理器的话，你选择的文件就不一定是图片了，这样的话，我们判断文件的后缀名
                     * 如果是图片格式的话，那么才可以  
                     */
                    if(path.endsWith("jpg")||path.endsWith("png"))
                    {
                        picPath = path;
                        Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
                        imageView.setImageBitmap(bitmap);
                    }else{alert();}
                }else{alert();}
                 
            } catch (Exception e) {
            }
        }
         
        super.onActivityResult(requestCode, resultCode, data);
    }
     
    private void alert()
    {
        Dialog dialog = new AlertDialog.Builder(this)
        .setTitle("提示")
        .setMessage("您选择的不是有效的图片")
        .setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int which) {
                        picPath = null;
                    }
                })
        .create();
        dialog.show();
    }
}